home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / ICONX_FO / RSYS.C < prev   
Text File  |  1990-03-02  |  4KB  |  211 lines

  1. /*
  2.  * File: rsys.c
  3.  *  Contents: getstrg, host, longread, putstr
  4.  */
  5.  
  6. #include "::h:config.h"
  7. #include "::h:rt.h"
  8. #include "rproto.h"
  9.  
  10. #if AMIGA
  11. #if LATTICE
  12. #include <ios1.h>
  13. #endif                    /* LATTICE */
  14. #endif                    /* AMIGA */
  15.  
  16. /*
  17.  * getstrg - read a line into buf from file fd.  At most maxi characters
  18.  *  are read.  getstrg returns the length of the line, not counting
  19.  *  the newline.  Returns -1 if EOF and -2 if length was limited by
  20.  *  maxi. [[ Needs ferror() check. ]]
  21.  */
  22.  
  23. int getstrg(buf, maxi, fd)
  24. register char *buf;
  25. int maxi;
  26. FILE *fd;
  27.    {
  28.    register int c, l;
  29.  
  30.  
  31. #if AMIGA
  32. #if LATTICE
  33.    /* This code is special for Lattice 4.0.  It was different for
  34.     *  Lattice 3.10 and probably won't work for other C compilers.
  35.     */
  36.    extern struct UFB _ufbs[];
  37.  
  38.    if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
  39.       return read(fileno(fd),buf,maxi);
  40. #endif                    /* LATTICE */
  41. #endif                    /* AMIGA */
  42.  
  43.  
  44.    l = 0;
  45.    while ((c = fgetc(fd)) != '\n') {
  46.       if (c == EOF)
  47.      if (l > 0) return l;
  48.      else return -1;
  49.       if (++l > maxi) {
  50.      ungetc(c, fd);
  51.      return -2;
  52.      }
  53.       *buf++ = c;
  54.       }
  55.    return l;
  56.    }
  57.  
  58. #ifdef UtsName
  59. #include <sys/utsname.h>
  60. #endif                    /* UtsName */
  61.  
  62. /*
  63.  * iconhost - return some sort of host name into the buffer pointed at
  64.  *  by hostname.  This code accommodates several different host name
  65.  *  fetching schemes.
  66.  */
  67. novalue iconhost(hostname)
  68. char *hostname;
  69.    {
  70.  
  71. #ifdef WhoHost
  72.    /*
  73.     * The host name is in /usr/include/whoami.h. (V7, 4.[01]bsd)
  74.     */
  75.    whohost(hostname);
  76. #endif                    /* WhoHost */
  77.  
  78. #ifdef UtsName
  79.    {
  80.    /*
  81.     * Use the uname system call.  (System III & V)
  82.     */
  83.    struct utsname utsn;
  84.    uname(&utsn);
  85.    strcpy(hostname,utsn.nodename);
  86.    }
  87. #endif                    /* UtsName */
  88.  
  89. #ifdef GetHost
  90.    /*
  91.     * Use the gethostname system call.  (4.2bsd)
  92.     */
  93.    gethostname(hostname,MaxCvtLen);
  94. #endif                    /* GetHost */
  95.  
  96. #if VMS
  97.    /*
  98.     * VMS has its own special logic.
  99.     */
  100.    char *h;
  101.    if (!(h = getenv("ICON$HOST")) && !(h = getenv("SYS$NODE")))
  102.       h = "VAX/VMS";
  103.    strcpy(hostname,h);
  104. #endif                    /* VMS */
  105.  
  106. #ifdef HostStr
  107.    /*
  108.     * The string constant HostStr contains the host name.
  109.     */
  110.    strcpy(hostname,HostStr);
  111. #endif                    /* HostStr */
  112.  
  113.    }
  114.  
  115. #ifdef WhoHost
  116. #define HdrFile "/usr/include/whoami.h"
  117.  
  118. /*
  119.  * whohost - look for a line of the form
  120.  *  #define sysname "name"
  121.  * in HdrFile and return the name.
  122.  */
  123. novalue whohost(hostname)
  124. char *hostname;
  125.    {
  126.    char buf[BUFSIZ];
  127.    FILE *fd;
  128.  
  129.    fd = fopen(HdrFile, "r");
  130.    if (fd == NULL) {
  131.       sprintf(buf, "Cannot open %s, no value for &host\n", HdrFile);
  132.       syserr(buf);
  133.    }
  134.  
  135.    for (;;) {   /* each line in the file */
  136.       if (fgets(buf, sizeof buf, fd) == NULL) {
  137.          sprintf(buf, "No #define for sysname in %s, no value for &host\n",
  138.             HdrFile);
  139.          syserr(buf);
  140.       }
  141.       if (sscanf(buf,"#define sysname \"%[^\"]\"", hostname) == 1) {
  142.          fclose(fd);
  143.          return;
  144.       }
  145.    }
  146.    }
  147. #endif                    /* WhoHost */
  148.  
  149. /*
  150.  * Read a long string in shorter parts. (Standard read may not handle long
  151.  *  strings.)
  152.  */
  153. word longread(s,width,len,fname)
  154. FILE *fname;
  155. int width;
  156. char *s;
  157. long len;
  158. {
  159.    long tally = 0;
  160.    long n = 0;
  161.  
  162.    while (len > 0) {
  163.       n = fread(s, width, (int)((len < MaxIn) ? len : MaxIn), fname);
  164.       if (n <= 0)
  165.          return tally;
  166.       tally += n;
  167.       s += n;
  168.       len -= n;
  169.       }  
  170.    return tally;
  171.    }
  172.  
  173. /*
  174.  * Print string referenced by descriptor d. Note, d must not move during
  175.  *   a garbage collection.
  176.  */
  177.  
  178. int putstr(f, d)
  179. register FILE *f;
  180. dptr d;
  181.    {
  182.    register char *s;
  183.    register word l;
  184.  
  185.    l = StrLen(*d);
  186.    if (l == 0)
  187.       return  Success;
  188.    s = StrLoc(*d);
  189.  
  190. #ifdef FixedRegions
  191.    if (longwrite(s,l,f) < 0)
  192.       return Failure;
  193.    else
  194.       return Success;
  195. #else                    /* FixedRegions */
  196.    /*
  197.     * In expandable regions storage management, the first output to a file may
  198.     *  cause allocation, which in turn may cause a garbage collection, changing
  199.     *  where the string is.  So write one character and reload the address
  200.     *  of the string from the tended descriptor.
  201.     */
  202.  
  203.    putc(*s, f);
  204.    s = StrLoc(*d) + 1;
  205.    if (longwrite(s,--l,f) < 0)
  206.       return Failure;
  207.    else
  208.       return Success;
  209. #endif                    /* FixedRegions */
  210.    }
  211.